var adds = {};

Page({

  data: {

    img_arr: [],
    img_arr2: [],
    formdata: '',
    textarea: "",
    uuid: "",
    fujianming: "",
    ifindex: 0,


  },

  //上传附件
  chooseMessageFile: function(e) {


    if (this.data.img_arr2.length < 1) {
      var that = this;
      wx.chooseMessageFile({
        count: 1,
        type: 'file',
        success(res) {
          console.log(".上传附件.." + res)
          var filename = res.tempFiles[0].name
          console.log("++++" + filename);
          that.setData({
            img_arr2: that.data.img_arr2.concat(res.tempFiles[0].path),
            fujianming: filename
          })
          console.log("???????" + that.data.img_arr2.length)
          console.log(res.tempFiles[0].path);
          that.setData({
            filename: filename
          });

        }
      })

    } else {
      wx.showToast({
        title: '只能上传一个附件',
        icon: 'loading',
        duration: 2000
      });
    }
  },




  //上传单个图片
  // fileUploadTap: function () {
  //   wx.chooseImage({
  //     success(res) {
  //       const tempFilePaths = res.tempFilePaths
  //       wx.uploadFile({
  //         url: 'http://192.168.1.7:8087/shuhai/upload', //仅为示例，非真实的接口地址
  //         filePath: tempFilePaths[0],
  //         name: 'file',
  //         formData: {
  //           description: '图片',
  //           i: '图片',
  //           c: '图片'
  //         },
  //         success(res) {
  //           console.log(res.data)
  //         }
  //       })
  //     }
  //   })
  // },

  formSubmit: function(e) {
    var id = e.target.id
    var that = this;
    var content = e.detail.value.textarea;


    if (content.replace(/(^s*)|(s*$)/g, "") == 0) {
      console.log("为空")
      wx.showToast({
        title: '请输入内容',
        icon: "none",
        duration: 3000
      });

      return false;
    }
    //生成id
    console.info(this.generateUuid());
    console.log(content);
    adds = e.detail.value;
    that.setData({
      textarea: content,
      uuid: this.generateUuid()
    })
    console.info(that.data.textarea);

    adds.program_id = e.jtappid
    adds.openid = e._openid
    adds.zx_info_id = this.data.zx_info_id
    //只有文字时提交
    if (that.data.img_arr.length == 0 && that.data.img_arr2.length == 0 && that.data.textarea != null) {
      console.log("第1次计入方法")
      this.wentimiaoshu();
      wx.showToast({
        title: '上传成功',
        icon: 'success',
        mask:true,//防止触摸穿透
        duration: 1500,
        success: function() {
          setTimeout(function() {
            wx.reLaunch({
              url: '/pages/index/index',
            })
          }, 1500);
        }
      })
   
    } else if (that.data.img_arr.length == 0) {
      console.log("第2次计入方法")
      this.upimgnoe();
      wx.showToast({
        title: '上传成功',
        icon: 'success',
        mask: true,//防止触摸穿透
        duration: 1500,
        success: function () {
          setTimeout(function () {
            wx.reLaunch({
              url: '/pages/index/index',
            })
          }, 1500);
        }
      })

    
    } else {
      console.log("第3次计入方法")
      this.upload();
      // that.setData({
      //   img_arr: [],
      //   img_arr2: [],
      //   textarea: "",
      //   fujianming: "",
      // })
      wx.showToast({
        title: '上传成功',
        icon: 'success',
        mask: true,//防止触摸穿透
        duration: 1500,
        success: function () {
          setTimeout(function () {
            wx.reLaunch({
              url: '/pages/index/index',
            })
          }, 1500);
        }
      })

    }
    console.log(adds);
  },

  //生成唯一不重复ID
  generateUuid: function(lengtdddh = 7) {
    return Number(Math.random().toString().substr(3, lengtdddh) + Date.now()).toString(36);
  },


  //上传照片
  upload: function() {
    console.log("上传照片")
    var that = this
    var imgfile;
    var name = wx.getStorageSync('name');
    for (var i = 0; i < that.data.img_arr.length; i++) { //循环遍历图片 
      console.log(that.data.img_arr[i])
      wx.uploadFile({
        url: 'https://gdzc.sxpmg.com/shuhai/upload.do', //自己的接口地址
        filePath: that.data.img_arr[i],
        name: 'file',

        formData: ({ //上传图片所要携带的参数
          name: name,
          id: that.data.uuid,
        }),
        success: function(res) {
          console.log(res)
        }
      })
    }
    if (that.data.img_arr2.length != 0) {
      this.upimgnoe();
    } else {

      this.wentimiaoshu();
    }
    this.setData({
      username: 'CMOS180404',
      password: 'ecb01ff6-2e5c-11e8-b467-0ed5f89f718b'

    })

  },


  //上传附件
  upimgnoe: function() {
    var that = this;
    var name = wx.getStorageSync('name');
    console.log("上传附件" + name)
    wx.uploadFile({
      url: "https://gdzc.sxpmg.com/shuhai/uploads.do",
      filePath: that.data.img_arr2[0],
      name: 'file',
      formData: {
        description: that.data.textarea,
        id: that.data.uuid,
        name: name,
      },
      success(res) {
        console.log(res);;
      }
    })
  },


  //上传图片
  upimg: function() {
    var that = this;
    console.log("计入方法")

    if (this.data.img_arr.length < 9) {
      wx.chooseImage({
        sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
        sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
        success: function(res) {
          var indes = res.tempFiles.length + that.data.img_arr.length

          that.setData({
            img_arr: that.data.img_arr.concat(res.tempFilePaths),
            ifindex: indes,
          });
        }
      })
    } else {
      wx.showToast({
        title: '图片只能9张',
        icon: 'loading',
        duration: 1000
      });
    }

  },



  //值上传提交问题描述
  wentimiaoshu: function() {
    var name = wx.getStorageSync('name');
    console.log("上传提交问题描述" + name)
    var that = this;
    wx.request({
      url: 'https://gdzc.sxpmg.com/shuhai/textpe.do',
      method: 'get',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        text: that.data.textarea,
        name: name,
        id: that.data.uuid,
      },
      success: function(res) {
        console.log(res.data);
        if (res.data.status == "success") {
          that.setData({
            img_arr: [],
            img_arr2: [],
            textarea: ""
          })
        }


      },
      fail: function(res) {
        wx.hideLoading();
        wx.showToast({
          title: '网络错误！',
          icon: 'none',
          duration: 2000
        })
        return;
      }
    });
  },

  //删除
  handSeir: function(e) {
    var that = this;
    var arr = that.data.img_arr;
    arr.splice(e.target.dataset.index, 1)
    that.setData({
      img_arr: arr,
      ifindex: that.data.img_arr.length,
    })

  },


})